package com.czk.yygh.user.controller;

import com.czk.model.user.UserInfo;
import com.czk.vo.user.LoginVo;
import com.czk.vo.user.UserAuthVo;
import com.czk.yygh.common.result.Result;
import com.czk.yygh.common.utils.AuthContextHolder;
import com.czk.yygh.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/18 22:25
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户信息管理")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 手机号登录
     *
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> map = userInfoService.login(loginVo);
        return Result.ok(map);
    }

    @PostMapping("/auth/userAuth")
    @ApiOperation(value = "用户认证")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        // 用户id
        Long userId = AuthContextHolder.getUserId(request);
        userInfoService.userAuth(userId, userAuthVo);
        return Result.ok();
    }

    @ApiOperation(value = "得到用户详细信息")
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
